#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

struct Record {
    string _word;
    int _frequency;
};

class Dictionary {
public:
    void read(const string &filename) {
        ifstream ifs(filename);
        if (!ifs) {
            cerr << "无法打开文件: " << filename << endl;
            return;
        }

        string word;
        while (ifs >> word) {
            // 查找单词是否已存在
            bool found = false;
            for (auto &rec : _dict) {
                if (rec._word == word) {  // 不转换大小写，严格匹配
                    rec._frequency++;
                    found = true;
                    break;
                }
            }
            if (!found) {
                _dict.push_back({word, 1});  // 新增记录
            }
        }
    }

    void store(const string &filename) {
        ofstream ofs(filename);
        if (!ofs) {
            cerr << "无法创建文件: " << filename << endl;
            return;
        }

        for (const auto &rec : _dict) {
            ofs << rec._word << " " << rec._frequency << endl;
        }
        cout << "结果已保存至 " << filename << endl;
    }

private:
    vector<Record> _dict;
};

int main() {
    string inputPath;
    cout << "请输入文件绝对路径: ";
    getline(cin, inputPath);

    Dictionary dict;
    dict.read(inputPath);
    dict.store("dict.txt");

    return 0;
}
